package hos.intent;

import android.app.Activity;
import android.content.ClipData;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * <p>Title: IntentResult </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-03-21 20:29
 */
public class IntentResult {

    public static Bitmap getBitmap(Intent intent) {
        if (intent == null) {
            return null;
        }
        Bitmap bitmap;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            bitmap = intent.getParcelableExtra("data", Bitmap.class);
        } else {
            bitmap = intent.getParcelableExtra("data");
        }
        if (bitmap != null) {
            return bitmap;
        }
        Bundle extras = intent.getExtras();
        if (extras == null) {
            return null;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            bitmap = extras.getParcelable("data", Bitmap.class);
        } else {
            bitmap = extras.getParcelable("data");
        }
        return bitmap;
    }

    public static Uri getMediaUri(Intent intent) {
        if (intent == null) {
            return null;
        }
        Uri uri = intent.getData();
        if (uri != null) {
            return uri;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            uri = intent.getParcelableExtra(MediaStore.EXTRA_OUTPUT, Uri.class);
        } else {
            uri = intent.getParcelableExtra(MediaStore.EXTRA_OUTPUT);
        }
        if (uri != null) {
            return uri;
        }
        Bundle extras = intent.getExtras();
        if (extras == null) {
            return null;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            uri = extras.getParcelable(MediaStore.EXTRA_OUTPUT, Uri.class);
        } else {
            uri = extras.getParcelable(MediaStore.EXTRA_OUTPUT);
        }
        return uri;
    }

    public static Uri getFileUri(Intent intent) {
        if (intent == null) {
            return null;
        }
        Uri uri = intent.getData();
        if (uri != null) {
            return uri;
        }
        ClipData clipData = intent.getClipData();
        if (clipData == null) {
            return null;
        }
        if (clipData.getItemCount() == 0) {
            return null;
        }
        return clipData.getItemAt(0).getUri();
    }

    private static List<Uri> getClipDataUris(Intent intent) {
        // Use a LinkedHashSet to maintain any ordering that may be
        // present in the ClipData
        LinkedHashSet<Uri> resultSet = new LinkedHashSet<>();
        if (intent.getData() != null) {
            resultSet.add(intent.getData());
        }
        ClipData clipData = intent.getClipData();
        if (clipData == null && resultSet.isEmpty()) {
            return Collections.emptyList();
        } else if (clipData != null) {
            for (int i = 0; i < clipData.getItemCount(); i++) {
                Uri uri = clipData.getItemAt(i).getUri();
                if (uri != null) {
                    resultSet.add(uri);
                }
            }
        }
        return new ArrayList<>(resultSet);
    }

    public static Uri getMediaUri(int resultCode, Intent intent, Uri uri) {
        try {
            if (resultCode != Activity.RESULT_OK) return null;
            if (intent == null) {
                return uri;
            }
            Uri intentUri = getMediaUri(intent);
            if (intentUri != null) {
                return intentUri;
            }
            if (uri != null) {
                return uri;
            }
            return getFileUri(intent);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Uri getFileUri(int resultCode, Intent intent) {
        try {
            if (resultCode != Activity.RESULT_OK) return null;
            return getFileUri(intent);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static List<Uri> getFileListUri(int resultCode, Intent intent) {
        try {
            if (resultCode != Activity.RESULT_OK) return null;
            if (intent == null) {
                return Collections.emptyList();
            }
            return getClipDataUris(intent);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
